﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SysMgr
{
    public partial class Config : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentConfig"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            btnNew.OnClientClick = Window1.GetShowReference("AddAgentPower.aspx", "新增服务担当索赔权限");

            //初始化
            //this.ddlSvrManagerUserId.DataSource = OUserController.GetUserByDepartment("'售后服务部'");
            //this.ddlSvrManagerUserId.DataBind();
            ///
           
            List<AgentPowerConfigInfo> list = new List<AgentPowerConfigInfo>();
            GetPowerList(list,0);
            this.grdSvr.DataSource = list;
            this.grdSvr.DataBind();
            ///
            
            //加载数据
           // this.ddlSvrManagerUserId.SelectedValue = AgentConfigController.SvrManagerUserId.ToString();
            this.tbxPartsManagerEmails.Text = AgentConfigController.PartsManagerEmails;
            this.tbxIHISCESvrSupperEmails.Text = AgentConfigController.IHISCESvrSupperEmails;
            this.QltyccZJLEmail.Text = AgentConfigController.QltyccZJLEmail;
            this.QltyccHWEmail.Text = AgentConfigController.QltyccHWEmail;
            this.QltyccBZEmail.Text = AgentConfigController.QltyccBZEmail;
            this.QltyccJSEmail.Text = AgentConfigController.QltyccJSEmail;
            this.QltyccSCEmail.Text = AgentConfigController.QltyccSCEmail;
            this.QltyccSCBEmail.Text = AgentConfigController.QltyccSCBEmail;

            this.QltyAlarmEmail.Text = AgentConfigController.QltyAlarmEmail;
            this.QltyAlarmMoney.Text = AgentConfigController.QltyAlarmMoney.ToString();


            this.VIPCustomerBuyPassEmails.Text = AgentConfigController.VIPCustomerBuyPassEmails;
            this.PWPartPassEmails.Text = AgentConfigController.PWPartPassEmails;
           // this.grdSvr.DataSource = OUserController.GetUserByDepartment("'售后服务部'");
            //this.grdSvr.DataBind();

        }

        private void GetPowerList(List<AgentPowerConfigInfo> list, int ParId)
        {
            List<AgentPowerConfigInfo> _parlist = AgentConfigController.GetByParId(ParId);
            if (_parlist.Count <= 0)
                return;
            foreach (AgentPowerConfigInfo itm in _parlist)
            {
                list.Add(itm);
                GetPowerList(list, itm.ID);
            }
        }
        protected void grdSvr_RowCommand(object sender, GridCommandEventArgs e)
        {
            int ID = GetSelectedDataKeyID(grdSvr);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!HasPower(Permission.删除))
                {
                    CheckPowerFailWithAlert();
                    return;
                }
                //执行删除代码
                if ( AgentConfigController.DeletePowerConfig(ID))
                {
                    LoadData();
                }
                else
                {
                    Alert.Show("删除失败");
                    return;
                }
            }
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
           // AgentConfigController.SvrManagerUserId = Convert.ToInt32(ddlSvrManagerUserId.SelectedValue);
            AgentConfigController.PartsManagerEmails = this.tbxPartsManagerEmails.Text.Trim();
            AgentConfigController.IHISCESvrSupperEmails = this.tbxIHISCESvrSupperEmails.Text.Trim();

            AgentConfigController.QltyccZJLEmail = this.QltyccZJLEmail.Text.Trim();
            AgentConfigController.QltyccHWEmail = this.QltyccHWEmail.Text.Trim();
            AgentConfigController.QltyccBZEmail = this.QltyccBZEmail.Text.Trim();
            AgentConfigController.QltyccJSEmail = this.QltyccJSEmail.Text.Trim();
            AgentConfigController.QltyccSCEmail = this.QltyccSCEmail.Text.Trim();
            AgentConfigController.QltyccSCBEmail = this.QltyccSCBEmail.Text.Trim();

            AgentConfigController.QltyAlarmEmail=this.QltyAlarmEmail.Text;
            AgentConfigController.QltyAlarmMoney =Convert.ToDecimal( this.QltyAlarmMoney.Text);

            AgentConfigController.VIPCustomerBuyPassEmails = this.VIPCustomerBuyPassEmails.Text.Trim();
            AgentConfigController.PWPartPassEmails = this.PWPartPassEmails.Text.Trim();
            ////保存服务担当 权限金额
            //for (int i = 0, count = this.grdSvr.Rows.Count; i < count; i++)
            //{
            //    int UserId = 0; 
            //    decimal ClaimAmount = 0m;
            //    UserId = Convert.ToInt32(this.grdSvr.DataKeys[i][0]);
            //    GridRow row = grdSvr.Rows[i];
            //    FineUI.TextBox nbClaimAmount = (FineUI.TextBox)row.FindControl("nbClaimAmount");
            //    ClaimAmount = Convert.ToDecimal(nbClaimAmount.Text.Trim());
            //    //
            //    OUserInfo obj=OUserController.GetById(UserId);
            //    obj.ClaimAmount=ClaimAmount;
            //    OUserController.Update(obj);
            //}
            //
            if (!AgentConfigController.SaveAll())
            {
                Alert.Show("修改系统配置信息失败！", "提示", MessageBoxIcon.Error);
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            LoadData();
        }

    }
}